package com.wbk.jobs;

import com.wbk.constant.RedisConstant;
import com.wbk.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){
        //获取垃圾图片名称集合
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff!=null){
            for (String filename:sdiff){
                //从七牛云中删除图片
                String picName = filename.substring(0, filename.lastIndexOf("."));
                System.out.println(picName);
                QiniuUtils.deleteFileFromQiniu(picName);
                //从Redis集合中删除图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,filename);
                System.out.println("垃圾图已删除："+filename);
            }
        }
    }
}
